package com.jin290.mqlibrary;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import static com.jin290.mqlibrary.CourseDialogAdapter.listToString1;


public class CourseActivity extends Activity implements View.OnClickListener {
    private GridView grv_course;
    private CourseAdapter courseAdapter;
    String course = "";

    String week = "";

    private List<CourseBean> courseBeenList;
    private CourseBean courseBean;
    SeleCoursectPicPopupWindow menuWindow;
    private LinearLayout activity_course;
    private ViewGroup root;

    private TextView tv_course_time1, tv_course_time2, tv_course_time3, tv_course_time4, tv_course_time5, tv_course_time6, tv_course_time7, tv_course_time8;
    private SheelPickerPopupWindow sheelPickerPopupWindow;
    private RelativeLayout rel_course_ok;
    private TextView tv_switch;
    private EditText editText;
    private ImageView back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        root = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_course, null);
        setContentView(root);


        initViews();

        Intent intent = getIntent();
        course = intent.getStringExtra("course");
        week = intent.getStringExtra("week");

        String[] ary = course.split(",");
        courseBeenList = new ArrayList<CourseBean>();
        for (String ss : ary) {
            courseBean = new CourseBean();
            courseBean.setCourse(ss);
            courseBeenList.add(courseBean);
        }
        String[] timeary = week.split(",");
        tv_course_time1.setText(timeary[0]);
        tv_course_time2.setText(timeary[1]);
        tv_course_time3.setText(timeary[2]);
        tv_course_time4.setText(timeary[3]);
        tv_course_time5.setText(timeary[4]);
        tv_course_time6.setText(timeary[5]);
        tv_course_time7.setText(timeary[6]);
        tv_course_time8.setText(timeary[7]);

        courseAdapter.getdata(courseBeenList);
        grv_course.setAdapter(courseAdapter);
        courseAdapter.notifyDataSetChanged();

//        HttpCourse();


    }


    private void initViews() {
        courseAdapter = new CourseAdapter(this);
        grv_course = (GridView) findViewById(R.id.grv_course);
        activity_course = (LinearLayout) findViewById(R.id.activity_course);
        rel_course_ok = (RelativeLayout) findViewById(R.id.rel_course_ok);
        tv_course_time1 = (TextView) findViewById(R.id.tv_course_time1);
        tv_course_time2 = (TextView) findViewById(R.id.tv_course_time2);
        tv_course_time3 = (TextView) findViewById(R.id.tv_course_time3);
        tv_course_time4 = (TextView) findViewById(R.id.tv_course_time4);
        tv_course_time5 = (TextView) findViewById(R.id.tv_course_time5);
        tv_course_time6 = (TextView) findViewById(R.id.tv_course_time6);
        tv_course_time7 = (TextView) findViewById(R.id.tv_course_time7);
        tv_course_time8 = (TextView) findViewById(R.id.tv_course_time8);
        tv_switch = (TextView) findViewById(R.id.tv_switch);
        back = (ImageView) findViewById(R.id.back);

        back.setOnClickListener(this);
        tv_course_time1.setOnClickListener(this);
        tv_course_time2.setOnClickListener(this);
        tv_course_time3.setOnClickListener(this);
        tv_course_time4.setOnClickListener(this);
        tv_course_time5.setOnClickListener(this);
        tv_course_time6.setOnClickListener(this);
        tv_course_time7.setOnClickListener(this);
        tv_course_time8.setOnClickListener(this);
        rel_course_ok.setOnClickListener(this);

        grv_course.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                courseBean = new CourseBean();
                Boolean cb = courseBeenList.get(position).isB();
                if (cb == false) {
                    courseBean.setB(true);
                } else {
                    courseBean.setB(false);
                }

                courseBean.setCourse(courseBeenList.get(position).getCourse());
                courseBeenList.set(position, courseBean);
                courseAdapter.notifyDataSetChanged();

                tv_switch.setVisibility(View.GONE);

                for (int i = 0; i < courseBeenList.size(); i++) {

                    if (courseBeenList.get(i).isB() == true) {

                        tv_switch.setVisibility(View.VISIBLE);
                        tv_switch.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {


                                menuWindow = new SeleCoursectPicPopupWindow(CourseActivity.this, itemsOnClick);
                                menuWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        menuWindow.setMposition(position);


                                        if (menuWindow.getdata().size() == 15) {
                                            return;
                                        }
                                        if (position == (menuWindow.getdata().size() - 1)) {
                                            editText = new EditText(CourseActivity.this);
                                            new AlertDialog.Builder(CourseActivity.this).setTitle("添加")
                                                    .setIcon(android.R.drawable.ic_dialog_info)
                                                    .setView(editText)
                                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog, int which) {

                                                            List<String> L;
                                                            L = menuWindow.getdata();
                                                            L.add((menuWindow.getdata().size() - 1), editText.getText().toString());
                                                            SPUtils.put(CourseActivity.this, "class", listToString1(L));
                                                            menuWindow.setdata(L);

                                                        }
                                                    }).setNegativeButton("取消", null).show();


                                        }


                                    }
                                });
                                //显示窗口
                                menuWindow.showAtLocation(CourseActivity.this.findViewById(R.id.activity_course),
                                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
                                        0); //设置layout在PopupWindow中显示的位置


                            }
                        });


                    }
                }


            }
        });


    }


    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            menuWindow.dismiss();
            week = "|" + tv_course_time1.getText() + "," + tv_course_time2.getText() + "," + tv_course_time3.getText() + "," + tv_course_time4.getText() + "," + tv_course_time5.getText() + "," + tv_course_time6.getText() + "," + tv_course_time7.getText() + "," + tv_course_time8.getText();
            int i1 = v.getId();
            if (i1 == R.id.tv_alert_ok) {
                tv_switch.setVisibility(View.GONE);
                System.out.println("menuWindow.getv()" + menuWindow.getv());
                for (int i = 0; i < courseBeenList.size(); i++) {

                    if (courseBeenList.get(i).isB() == true) {
                        courseBean = new CourseBean();
                        courseBean.setCourse(menuWindow.getv());
                        courseBean.setB(false);
                        courseBeenList.set(i, courseBean);
                        courseAdapter.notifyDataSetChanged();
                    }


                }
                StringBuilder result = new StringBuilder();
                boolean flag = false;
                for (int i = 0; i < courseBeenList.size(); i++) {
                    if (flag) {
                        result.append(",");
                    } else {
                        flag = true;
                    }
                    result.append(courseBeenList.get(i).getCourse());
                }
                course = result.toString();
//                    HttpCourse(course + week);

            } else if (i1 == R.id.tv_alert_dele) {
                tv_switch.setVisibility(View.GONE);
                for (int i = 0; i < courseBeenList.size(); i++) {

                    if (courseBeenList.get(i).isB() == true) {
                        courseBean = new CourseBean();
                        courseBean.setCourse("-");
                        courseBean.setB(false);
                        courseBeenList.set(i, courseBean);
                        courseAdapter.notifyDataSetChanged();
                    }

                }


                StringBuilder result1 = new StringBuilder();
                boolean flag1 = false;
                for (int i = 0; i < courseBeenList.size(); i++) {
                    if (flag1) {
                        result1.append(",");
                    } else {
                        flag1 = true;
                    }
                    result1.append(courseBeenList.get(i).getCourse());
                }


//                    HttpCourse(result1.toString() + week);


            } else if (i1 == R.id.tv_alert_cancel) {
                menuWindow.dismiss();
                menuWindow = null;

            } else {
            }


        }

    };


    @Override
    public void onClick(View v) {

        int i1 = v.getId();
        if (i1 == R.id.back) {
            finish();

        } else if (i1 == R.id.rel_course_ok) {//Button 确定 获取数据 操作

            StringBuilder result = new StringBuilder();
            boolean flag = false;
            for (int i = 0; i < courseBeenList.size(); i++) {
                if (flag) {
                    result.append(",");
                } else {
                    flag = true;
                }
                result.append(courseBeenList.get(i).getCourse());
            }

            week = tv_course_time1.getText() + "," + tv_course_time2.getText() + "," + tv_course_time3.getText() + "," + tv_course_time4.getText() + "," + tv_course_time5.getText() + "," + tv_course_time6.getText() + "," + tv_course_time7.getText() + "," + tv_course_time8.getText();
            course = result.toString();
            SPUtils.put(CourseActivity.this, "week", week);
            SPUtils.put(CourseActivity.this, "course", course);


            Intent intent = new Intent();
            intent.putExtra("week", week);
            intent.putExtra("course", course);
            setResult(10086, intent);
            finish();



        } else if (i1 == R.id.tv_course_time1) {
            getsheelPickerPopupWindow(tv_course_time1);

        } else if (i1 == R.id.tv_course_time2) {
            getsheelPickerPopupWindow(tv_course_time2);


        } else if (i1 == R.id.tv_course_time3) {
            getsheelPickerPopupWindow(tv_course_time3);


        } else if (i1 == R.id.tv_course_time4) {
            getsheelPickerPopupWindow(tv_course_time4);


        } else if (i1 == R.id.tv_course_time5) {
            getsheelPickerPopupWindow(tv_course_time5);


        } else if (i1 == R.id.tv_course_time6) {
            getsheelPickerPopupWindow(tv_course_time6);


        } else if (i1 == R.id.tv_course_time7) {
            getsheelPickerPopupWindow(tv_course_time7);


        } else if (i1 == R.id.tv_course_time8) {
            getsheelPickerPopupWindow(tv_course_time8);

        }

    }


    //为弹出窗口实现监听类
    private View.OnClickListener itemsSheelPickerOnClick = new View.OnClickListener() {

        public void onClick(View v) {

            sheelPickerPopupWindow.dismiss();
            StringBuilder result = new StringBuilder();
            boolean flag = false;
            for (int i = 0; i < courseBeenList.size(); i++) {
                if (flag) {
                    result.append(",");
                } else {
                    flag = true;
                }
                result.append(courseBeenList.get(i).getCourse());
            }
            course = result.toString();


            int i = v.getId();
            if (i == R.id.tv_alert_ok) {
                try {
                    sheelPickerPopupWindow.gettext();
                    week = "|" + tv_course_time1.getText() + "," + tv_course_time2.getText() + "," + tv_course_time3.getText() + "," + tv_course_time4.getText() + "," + tv_course_time5.getText() + "," + tv_course_time6.getText() + "," + tv_course_time7.getText() + "," + tv_course_time8.getText();


//                        HttpCourse(course + week);


                } catch (ParseException e) {
                    e.printStackTrace();
                }


            } else if (i == R.id.tv_alert_delete) {
                sheelPickerPopupWindow.Deletetext();
                week = "|" + tv_course_time1.getText() + "," + tv_course_time2.getText() + "," + tv_course_time3.getText() + "," + tv_course_time4.getText() + "," + tv_course_time5.getText() + "," + tv_course_time6.getText() + "," + tv_course_time7.getText() + "," + tv_course_time8.getText();
//                    HttpCourse(course + week);

            }

        }

    };

    private void getsheelPickerPopupWindow(TextView textView) {
        sheelPickerPopupWindow = new SheelPickerPopupWindow(CourseActivity.this, textView, itemsSheelPickerOnClick);
        //显示窗口
        sheelPickerPopupWindow.showAtLocation(CourseActivity.this.findViewById(R.id.activity_course),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
                0); //设置layout在PopupWindow中显示的位置
    }


}
